package com.tianyang.yukuaifu.business.auth.fragment;

import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifu.R;
import com.tianyang.yukuaifu.base.BaseFragment;
import com.tianyang.yukuaifu.business.auth.adapter.MerchantCheckAdapter;
import com.tianyang.yukuaifu.business.auth.contract.MerchantCheckContract;
import com.tianyang.yukuaifu.business.auth.presenter.MerchantCheckPresenter;
import com.tianyang.yukuaifu.business.main.bean.PosUserOutput;
import com.tianyang.yukuaifu.common.interfaces.CommonConfig;
import com.tianyang.yukuaifu.module.widget.VerticalSwipeRefreshLayout;

import java.util.List;

import butterknife.BindView;

/**
 * Description：商户审核中
 * Created by xyj on 2024/3/8
 */
public class MerchantCheckFragment extends BaseFragment implements MerchantCheckContract.View {

    @BindView(R.id.phoneNumberEt)
    EditText phoneNumberEt;
    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;
    @BindView(R.id.emptyView)
    RelativeLayout emptyView;

    private MerchantCheckAdapter vMerchantCheckAdapter;
    
    private MerchantCheckContract.Presenter mPresenter;

    private int nowPage = CommonConfig.PAGE_NUM;

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_merchant_submit;
    }

    @Override
    protected void initView() {
        super.initView();
        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vMerchantCheckAdapter = new MerchantCheckAdapter();
        vRecyclerview.setAdapter(vMerchantCheckAdapter);
        vMerchantCheckAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vMerchantCheckAdapter.setPreLoadNumber(2);

        mPresenter = new MerchantCheckPresenter(this);
        mPresenter.getMerchantCheckList(nowPage, phoneNumberEt.getText().toString());
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.getMerchantCheckList(nowPage, phoneNumberEt.getText().toString());
                vMerchantCheckAdapter.setEnableLoadMore(true);
            }
        });
        vMerchantCheckAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getMerchantCheckList(nowPage, phoneNumberEt.getText().toString());
            }
        }, vRecyclerview);
        vMerchantCheckAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.queryStatusTv) {
                    PosUserOutput output = ((MerchantCheckAdapter) adapter).getItem(position);
                    if (output != null) {
                        mPresenter.getIdcertconfirm(output.getPhone(), output.getType());
                    }
                }
            }
        });
    }

    @Override
    public void showSaveSuccess() {
        nowPage = CommonConfig.PAGE_NUM;
        mPresenter.getMerchantCheckList(nowPage, phoneNumberEt.getText().toString());
        vMerchantCheckAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showMerchantCheckListView(List<PosUserOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vMerchantCheckAdapter.setNewData(outputs);
            } else {
                vMerchantCheckAdapter.addData(outputs);
            }
            nowPage++;
            vRecyclerview.setVisibility(View.VISIBLE);
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vRecyclerview.setVisibility(View.GONE);
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vMerchantCheckAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vMerchantCheckAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vMerchantCheckAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MerchantCheckContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
